/***********************************************************************
Copyright 2020 GuYueHome (www.guyuehome.com).
***********************************************************************/

//两种集合通信方式

/**
 * 该例程监听tf数据，并计算、发布turtle2的速度指令
 */

#include <ros/ros.h>
#include <tf/transform_listener.h>
#include <geometry_msgs/Twist.h>
#include <turtlesim/Spawn.h>
#include "turtlesim/Pose.h"

static double x1,yl,yaw1,pitch1,roll1,     x2,y2,yaw2,pitch2,roll2,
         	           dx,dy,dyaw       ,dpitch,droll, yaw, pitch, roll,
						ex,ey,eyaw,
          	          v1,omega1,v2,omega2,
         	           K1,K2,K3;

static ros::Subscriber sub1;
static ros::Subscriber sub2;
static ros::Publisher turtle_vel;


void doPose1(const turtlesim::Pose::ConstPtr& p){
	x1=p->x;
	yl=p->y;
	yaw1=p->theta;
	v1=p->linear_velocity;
	omega1=p->angular_velocity;
}

// 方法一：发布在订阅中运行

// void doPose2(const turtlesim::Pose::ConstPtr& p){
// 	x2=p->x;
// 	y2=p->y;
// 	yaw2=p->theta;
// 	v2=p->linear_velocity;
// 	omega2=p->angular_velocity;

// 	dx=x1-x2;
// 	dy=yl-y2;
// 	dyaw=yaw1-yaw2;

// 	ROS_INFO("乌龟1位姿信息:x=%.2f,y=%.2f,theta=%.2f,lv=%.2f,av=%.2f",
// 	x1,yl,yaw1,v1,omega1);
// 	ROS_INFO("乌龟2位姿信息:x=%.2f,y=%.2f,theta=%.2f,lv=%.2f,av=%.2f",
// 	x2,y2,yaw2,v2,omega2);

// 	geometry_msgs::Twist vel_msg;
// 	vel_msg.angular.z = -0.40 * atan2(dy,dx);
// 	vel_msg.linear.x = 0.1 * sqrt(pow(dx, 2) +pow(dy, 2));
// 	ROS_INFO("乌龟2命令:x=%.2f,theta=%.2f:",
// 	vel_msg.linear.x,vel_msg.angular.z);

// 	turtle_vel.publish(vel_msg);


// }


// int main(int argc, char** argv)
// {
// 	setlocale(LC_ALL,"");

// 	// 初始化ROS节点
// 	ros::init(argc, argv, "my_tf_listener");

//     // 创建节点句柄
// 	ros::NodeHandle node;

// 	// 请求产生turtle2
// 	ros::service::waitForService("/spawn");
// 	ros::ServiceClient add_turtle = node.serviceClient<turtlesim::Spawn>("/spawn");
// 	turtlesim::Spawn srv;
// 	add_turtle.call(srv);

// 	sub1 = node.subscribe<turtlesim::Pose>("/turtle1/pose",1000,doPose1);
// 	sub2 = node.subscribe<turtlesim::Pose>("/turtle2/pose",1000,doPose2);
// 	// 创建发布turtle2速度控制指令的发布者
// 	turtle_vel = node.advertise<geometry_msgs::Twist>("/turtle2/cmd_vel", 10);
// 	ros::spin();
// 	return 0;
// };

void doPose2(const turtlesim::Pose::ConstPtr& p){
	x2=p->x;
	y2=p->y;
	yaw2=p->theta;
	v2=p->linear_velocity;
	omega2=p->angular_velocity;





	// geometry_msgs::Twist vel_msg;
	// vel_msg.angular.z = -0.40 * atan2(dy,dx);
	// vel_msg.linear.x = 0.1 * sqrt(pow(dx, 2) +pow(dy, 2));
	// ROS_INFO("乌龟2命令:x=%.2f,theta=%.2f:",
	// vel_msg.linear.x,vel_msg.angular.z);
	// turtle_vel.publish(vel_msg);
}

// 方法一：订阅在发布中运行

int main(int argc, char** argv)
{
	setlocale(LC_ALL,"");

	// 初始化ROS节点
	ros::init(argc, argv, "my_tf_listener");

    // 创建节点句柄
	ros::NodeHandle node;

	// 请求产生turtle2
	ros::service::waitForService("/spawn");
	ros::ServiceClient add_turtle = node.serviceClient<turtlesim::Spawn>("/spawn");
	turtlesim::Spawn srv;
	add_turtle.call(srv);

	sub1 = node.subscribe<turtlesim::Pose>("/turtle1/pose",1000,doPose1);
	sub2 = node.subscribe<turtlesim::Pose>("/turtle2/pose",1000,doPose2);
	// 创建发布turtle2速度控制指令的发布者
	turtle_vel = node.advertise<geometry_msgs::Twist>("/turtle2/cmd_vel", 10);

	// 创建tf的监听器
	tf::TransformListener listener;

	ros::Rate rate(10);
	while (node.ok())
	{

		// 获取turtle1与turtle2坐标系之间的tf数据
		tf::StampedTransform transform;
		try
		{
			listener.waitForTransform("/turtle2", "/turtle1", ros::Time(0), ros::Duration(3.0));
			listener.lookupTransform("/turtle2", "/turtle1", ros::Time(0), transform);
		}




		catch (tf::TransformException &ex) 
		{
			ROS_ERROR("%s",ex.what());
			ros::Duration(1.0).sleep();
			continue;
		}

		dx=x1-x2;
		dy=yl-y2;
		dyaw=yaw1-yaw2;
		transform.getBasis().getEulerYPR(eyaw, pitch, roll);


		ROS_INFO("乌龟1位姿信息:x=%.2f,y=%.2f,theta=%.2f,lv=%.2f,av=%.2f",
		x1,yl,yaw1,v1,omega1);
		ROS_INFO("乌龟2位姿信息:x=%.2f,y=%.2f,theta=%.2f,lv=%.2f,av=%.2f",
		x2,y2,yaw2,v2,omega2);
		ROS_INFO("乌龟位姿差:dx=%.2f,dy=%.2f,dtheta=%.2f",
		transform.getOrigin().x()-dx,transform.getOrigin().y()-dy,eyaw-dyaw);

		// 根据turtle1与turtle2坐标系之间的位置关系，发布turtle2的速度控制指令
		geometry_msgs::Twist vel_msg;



		vel_msg.angular.z = 4.0 * atan2(transform.getOrigin().y(),
				                        transform.getOrigin().x());
		vel_msg.linear.x = 0.5 * sqrt(pow(transform.getOrigin().x(), 2) +
				                      pow(transform.getOrigin().y(), 2));


		turtle_vel.publish(vel_msg);


		rate.sleep();
		ros::spinOnce();
	}
	return 0;
};